<?php
class SiteController extends AdminController {
	public function SiteController(){
		parent::AdminController();
		$this->page_title = '管理平台';
	}
	
	public function index(){
		$this->display();
	}
	
	public function manager(){
		$this->init_page_info();
		//获取所有的网站列表
		$site_mod = new SiteModel();
		$site_list =  $site_mod->find(array('_id'=>array('$ne'=>SITE_ID)));
		foreach($site_list as &$site){
			$site['domain'] = $site['domains'][0];
		}
		if(!isset($this->user_info['ticket'])){
			global $session_key;
			$ticket = encrypt($session_key . ';' . SITE_ID . ';' . $this->user_info['_id']);
			$ticket = urlencode($ticket);
		}else{
			$ticket = $this->user_info['ticket'];
		}
		$this->assign('ticket', $ticket);
		$this->set_page_total($site_mod->count() - 1);
		$this->assign('site_list', $site_list);
		$this->display();
	}
	
	public function acl(){
		if(empty($this->id)){
			self::redirect('/index.php?app=system&act=site');
		}
		$site_mod = new SiteModel();
		$acl_mod = new AclModel();
		if(IS_POST){
			//获取权限值
			$acl_array = get_acl_array($acl_mod);
			
			//保存
			$site_mod->update_by_key($this->id, array('acl'=>$acl_array));
			$cache = get_cache_service();
			$cache->remove($this->id);
		}
		
		$site = $site_mod->get_by_key($this->id, 'logo,name,acl');
		self::assign('site', $site);
		
		//加载权限
		$acls = $acl_mod->find();
		if(isset($site['acl'])){
			ACLHelper::format_acls_checkbox($acls, $site['acl']);
		}
		$this->assign('acls', $acls);
		$this->display();
	}

	public function login(){
		if(!empty($this->user_info) && $this->user_info['has_login']){
			$this->redirect('/admin.php');
		}
		//验证码验证
		$captcha_request = get_session('captcha_request');
		$error_count = get_session('error_count');
		if($error_count>5){
			echo '错误登录次数过多，30分钟内禁止登录！';
			exit;
		}
		if(IS_POST){
			$user_name = trim($_POST['user_name']);
			$password = trim($_POST['password']);
			if(empty($user_name)){
				$this->ajax_login(false, '用户名不能为空！');
			}
			if(empty($password)){
				$this->ajax_login(false, '密码不能为空！');
			}
			if(!empty($captcha_request) && $captcha_request){
				$captcha = trim($_POST['captcha']);
				require_once ROOT_PATH . '/protected/includes/securimage/securimage.php';
				$securimage = new Securimage;
				if($securimage->check($captcha)==false){
					$this->ajax_login(false, '验证码错误！');
				}
			}
			//用户登录
			$result = UCenter::login(SITE_ID, $user_name, $password);
			if(!$result['result']){
				//登录失败
				$this->ajax_login(false, $result['info']);
			}else{
				//登录成功
				$this->do_login($result['data']);
				$this->ajax_login(true, $result['info']);
			}
		}
		if($error_count>3){
			$this->assign('captcha_request', true);
		}
		$this->display();
	}
	
	public function logout(){
		UCenter::logout($this->user_info['_id']);
		remove_session();
		$this->redirect('/admin.php?act=login');
	}

	private function ajax_login($result, $info){
		if(!$result){
			//如果是登录失败，则错误加1
			$error_count = get_session('error_count');
			if(empty($error_count))
				$error_count = 0;
			++$error_count;
			set_session('error_count', $error_count);
			if($error_count > 3){
				//要求显示验证码
				set_session('captcha_request', true);
				$this->assign('captcha_request', true);
			}
		}else{
			$error_count = 0;
		}
		echo json_encode(array(
				'result' 	=> $result,
				'info'		=> $info,
				'error_count'=> $error_count,
		));
		exit;
	}
	
	private function do_login($user, $new_session_key=null){
		$user['has_login'] = true;
		unset($user['error_count']);
		unset($user['captcha_request']);
		
		if(!empty($new_session_key)){
			//更新Session过期日期
			$session_expire = time() + intval(SESSION_EXPIRE);
			global $session_key;
			$session_key = $new_session_key;
			setcookie('SESSION_ID', $session_key , $session_expire, '/');
		}
		
		//保存Session信息
		set_session('visitor', $user);
	}
	
	public function edit(){
		if(IS_POST){
			$this->save();
		}
		$this->assign('status', SiteModel::get_status());
		$this->display();
	}
	
	private function save(){
		$form = new AttrHelper(array('form_name'=>'site_form'));
		$result = $form->get_post_data();
		if($result['result'] === false){
			$this->ajax_error($result['info']);
		}
		
		$domein = $result['data']['domain'];
		if (!preg_match('/[[a-z|A-Z][a-z|A-Z|0-9|_]+/i', $domein)) {
			self::ajax_error('域名不合法，只允许使用字母、数字和下划线！');
		}
		
		global $site_info;
		$langs = $site_info['langs'];
		$muti_lan_form_names = array('name', 'description', 'keywords');
		$site = array(
			'domains' => array(
				$domein . '.duoec.com',
			),
			'name' => $result['data'][LANGUAGE]['name'],
			'default_lang' => LANGUAGE,
			'langs' => $langs,
			'status' => $result['data']['status'],
		);
		foreach ($langs as $lan=>$is_default){
			foreach ($muti_lan_form_names as $form_name){
				$site[$lan][$form_name] = $result['data'][$lan][$form_name];
			}
			$name = $site[$lan]['name'];
			if(utf8_strlen($name)>50){
				$this->ajax_error('名称长度应该在1至50之间！'.$name);
			}
		}
		
		$admin_name = $result['data']['admin_name'];
		if(empty($admin_name) || utf8_strlen($admin_name)<4 || utf8_strlen($admin_name)>50){
			$this->ajax_error('管理员名称长度应该在4至50之间！');
		}
		
		$email = $result['data']['email'];
		if(!validate_email($email)){ 
			$this->ajax_error('管理员邮箱不正确！');
		}
		
		$password = $result['data']['password'];
		$password_confirm = $result['data']['password_confirm'];
		if($password !== $password_confirm){
			$this->ajax_error('两次密码输入不一至！');
		}
		if(!isset($password) || strlen($password)<6){
			$this->ajax_error('用户密码长度太短！');
		}
		
		
		$site_mod = new SiteModel();
		if(empty($this->id)){
			//新建
			$site_id = gen_guid();
			$site['_id'] = $site_id;
			$site_mod->insert($site);
			
			UCenter::add_user(array(
				'user_name' => $admin_name,
				'site_id' => $site_id,
				'email' => $email,
				'password' => $password,
				'roles_yulin' => array('9E6CB90CA23A4007BB2FE7E3BBC68FB7'),
			));
			$this->ajax_info('添加成功！');
		}else{
			//修改
			$this->ajax_error('暂未实现编辑功能！');
		}
	}
}

?>